class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        if(pushed == null || pushed.length == 0) {
            return true;
        }
        int a = 0;
        for(int i = 0;i < pushed.length;i++) {
            stack.push(pushed[i]);
            while(!stack.empty() && (stack.peek() == popped[a])) {
                stack.pop();
                a++; 
            }
            
        }
        for(int i = a;i < popped.length;i++) {
            if(stack.pop() != popped[i]) {
                return false;
            } 
        }
        return true;
    }
}